// curl -u email:password -d status="your message here" http://twitter.com/statuses/update.xml

// This is the url we post to.
var baseUrl = "twitter.com/statuses/update.xml";

var userName = "";
var password = "";

//
// Function: load()
// Called by HTML body element's onload event when the widget is ready to start
//
function load()
{
    setupParts();
    loadPrefs();
    refreshClick();
}

//
// Function: remove()
// Called when the widget has been removed from the Dashboard
//
function remove()
{
    // Stop any timers to prevent CPU usage
    // Remove any preferences as needed
    // widget.setPreferenceForKey(null, createInstancePreferenceKey("your-key"));
}

//
// Function: hide()
// Called when the widget has been hidden
//
function hide()
{
    // Stop any timers to prevent CPU usage
}

//
// Function: show()
// Called when the widget has been shown
//
function show()
{
    refreshClick();
    // Restart any timers that were stopped on hide
}

//
// Function: sync()
// Called when the widget has been synchronized with .Mac
//
function sync()
{
    // Retrieve any preference values that you need to be synchronized here
    // Use this for an instance key's value:
    // instancePreferenceValue = widget.preferenceForKey(null, createInstancePreferenceKey("your-key"));
    //
    // Or this for global key's value:
    // globalPreferenceValue = widget.preferenceForKey(null, "your-key");
}

//
// Function: showBack(event)
// Called when the info button is clicked to show the back of the widget
//
// event: onClick event from the info button
//
function showBack(event)
{
    var front = document.getElementById("front");
    var back = document.getElementById("back");

    if (window.widget) {
        widget.prepareForTransition("ToBack");
    }

    front.style.display = "none";
    back.style.display = "block";

    if (window.widget) {
        setTimeout('widget.performTransition();', 0);
    }
}

//
// Function: showFront(event)
// Called when the done button is clicked from the back of the widget
//
// event: onClick event from the done button
//
function showFront(event)
{
    var front = document.getElementById("front");
    var back = document.getElementById("back");

    if (window.widget) {
        widget.prepareForTransition("ToFront");
    }

    front.style.display="block";
    back.style.display="none";
    
    // get the setup parameters
    userName = document.getElementById("txtUserName").value;
    password = document.getElementById("txtPassword").value;
    savePrefs();
    refreshClick();
    if (window.widget) {
        setTimeout('widget.performTransition();', 0);
    }
}

if (window.widget) {
    widget.onremove = remove;
    widget.onhide = hide;
    widget.onshow = show;
    widget.onsync = sync;
}


function refreshClick(event){
/*
[{
    "in_reply_to":null,
    "created_at":"Thu Apr 10 19:48:59 +0000 2008",
    "truncated":false,
    "in_reply_to_user_id":null,
    "text":"Trying to figure out why his webdav broked.",
    "source":"web",
    "id":786687913,
    "user":{
        "name":"lmeadors",
        "followers_count":2,
        "url":null,
        "description":"",
        "id":14338550,
        "location":"",
        "screen_name":"lmeadors",
        "protected":true,
        "profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/52582351\/lego-boba-fett_normal.png"
    }
}]
*/
    if(!userName || userName.length == 0){
        document.getElementById("txtContent").innerHTML = "Click on the i (upper right hand corner) and "+
        "give me your username and password. Please.";
        return;
    }
    if(!password || password.length == 0){
        document.getElementById("txtContent").innerHTML = "Click on the i (upper right hand corner) and "+
        "give me your username and password. Please.";
        return;
    }
    var urlToLoad = "twitter.com/statuses/friends_timeline.json";
    $("#txtContent").html("Loading: http://" + urlToLoad);
    $.getJSON("http://"+userName+":"+password+"@"+urlToLoad, setStatus);
}

function setStatus(data){
    var statusList="";
    var i;
    var status;
    if(document.getElementById("txtContent")){
        for(i=0;i<data.length;i++){
            status = data[i];
            if(null != status.in_reply_to_user_id) alert(status.text.substr(1, userName.length));
            if(status.text.substr(1, userName.length) == userName){
                statusList += "<a class='replyToMe' href='javascript:replyTo(\"" + 
                    status.user.name + "\")'>" + status.user.name + ": " + 
                    status.text + " <span class='timestamp'>(" + 
                    status.created_at + ")</span></a><hr/>";
            }else{
                statusList += "<a class='replyTo' href='javascript:replyTo(\"" + 
                    status.user.name + "\")'>" + status.user.name + ": " + 
                    status.text + " <span class='timestamp'>(" + 
                    status.created_at + ")</span></a><hr/>";
            }
        }
        document.getElementById("txtContent").innerHTML = statusList; // jQuery dies here for some reason. gay.
    }
}

function replyTo(sUserName){
    document.getElementById("txtStatus").value = "@" + sUserName + " ";
}

function updateClick(event){
    var postUrl = "http://" + userName + ":" + password + "@twitter.com/statuses/update.xml";
    $.post(postUrl, {"status":document.getElementById("txtStatus").value}, postUpdate);
    document.getElementById("txtStatus").value = "";
    updateCountIndicators();
}

function savePrefs(){
    widget.setPreferenceForKey(userName, "twit.userName");
    widget.setPreferenceForKey(password, "twit.password");
}

function loadPrefs(){
    userName = widget.preferenceForKey("twit.userName");
    password = widget.preferenceForKey("twit.password");
    if(document.getElementById("txtUserName")) document.getElementById("txtUserName").value = userName;
    if(document.getElementById("txtPassword")) document.getElementById("txtPassword").value = password;
}

function postUpdate(data, textStatus) {
    if(textStatus = "success"){
        document.getElementById("txtStatus").value = "";
        refreshClick(null);
    }else{
        $("#txtContent").html(textStatus);
    }
}
function updateCountIndicators(){
    var length = document.getElementById("txtStatus").value.length + 1;
    var remaining = 140 - length;
    document.getElementById("txtCount").innerHTML = "Used " + length + " / 140 : " + remaining + " remain.";
    document.getElementById("gauge").object.setValue(length);
}

function submitOnEnter(event){
    updateCountIndicators();
    if(event.keyCode == 13){
        updateClick();
    }
}


function checkSize(event){
/*
    var length = document.getElementById("txtStatus").value.length + 1;
        if(length > 139) {
        event.cancelBubble;
        event.preventDefault();
        event.stopPropagation();
        return true;
    }
*/
}
